package com.zhulanhua.blogs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhulanhua.blogs.Utils.DozerUtils;
import com.zhulanhua.blogs.Utils.RequestContextUtils;
import com.zhulanhua.blogs.Utils.StringUtils;
import com.zhulanhua.blogs.controller.VO.ArticleDetailVO;
import com.zhulanhua.blogs.controller.VO.ArticleVO;
import com.zhulanhua.blogs.dao.ArticleDao;
import com.zhulanhua.blogs.dao.UserDao;
import com.zhulanhua.blogs.domain.Article;
import com.zhulanhua.blogs.domain.query.ArticleQuery;
import com.zhulanhua.blogs.service.ArticleService;
import com.zhulanhua.blogs.service.CommentService;
import com.zhulanhua.blogs.service.ImagesService;
import com.zhulanhua.blogs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    ArticleDao articleDao;

    @Autowired
    CommentService commentService;

    @Autowired
    ImagesService imagesService;

    @Autowired
    UserService userService;

    @Autowired
    DozerUtils dozerUtils;

    @Override
    public Map<String, Integer> getMap() {
        List<Article> articles = articleDao.selectList(null);
        int supports = 0;
        int browses = 0;
        for (Article article : articles){
            supports += article.getSupport();
            browses += article.getBrowse();
        }
        HashMap<String, Integer> map = new HashMap<>();
        map.put("support", supports);
        map.put("browse", browses);
        map.put("count", articles.size());
        return map;
    }

    @Override
    public List<ArticleVO> getQuery(ArticleQuery articleQuery) {
        QueryWrapper<Article> qw = new QueryWrapper<>();
        Page<Article> page = null;
        if (articleQuery.getPage() == 0 || articleQuery.getPageNum() == 0){
            page = new Page<>();
        }else {
            page = new Page<>(articleQuery.getPage(), articleQuery.getPageNum());
        }
        qw.select("article_id, article_title, user_id, simple, icon, article_type, support, browse, create_time");
        //筛选条件
        qw.lambda()
          .gt(articleQuery.getMinTime() != null, Article::getCreateTime, articleQuery.getMinTime())
          .gt(articleQuery.getMinSupport() != 0, Article::getSupport, articleQuery.getMinSupport())
          .gt(articleQuery.getMinBrowse() != 0, Article::getSupport, articleQuery.getMinSupport())
          .lt(articleQuery.getMaxBrowse() != 0, Article::getCreateTime, articleQuery.getMaxTime())
          .lt(articleQuery.getMaxTime() != null, Article::getCreateTime, articleQuery.getMaxTime())
          .lt(articleQuery.getMaxSupport() != 0, Article::getSupport, articleQuery.getMaxSupport());

        //通过关键字搜索
        if (StringUtils.isNotEmpty(articleQuery.getKey())){
            qw.lambda().and(q -> {
                q.or();
                q.like(Article::getArticleTitle, articleQuery.getKey());
                q.or();
                q.like(Article::getSimple, articleQuery.getKey());
                q.or();
                q.like(Article::getSearchKey, articleQuery.getKey());
            });
        }

        //文章类型
        if (articleQuery.getType() != null && articleQuery.getType().length > 0){
            qw.lambda().and(q -> {
                for (String s : articleQuery.getType()){
                    q.or();
                    q.like(Article::getArticleType, s);
                }
            });
        }



        //排序条件
        if (articleQuery.getSort() != null){
            String[] sort = articleQuery.getSort().split(",");
            qw.orderByAsc(sort[0].equals("asc"), articleQuery.getList());
            qw.orderByDesc(sort[0].equals("desc"), articleQuery.getList());
        }

        List<Article> records1 = articleDao.selectPage(page, qw).getRecords();

        List<ArticleVO> records = dozerUtils.convert(records1, ArticleVO.class);

        //查询其他信息
        for (ArticleVO art : records){
            art.setCommentSum(commentService.getCount(art.getArticleId()));
            art.setUser(userService.getInfo(art.getUserId()));
            art.setArticleTypeArray(art.getArticleType().split(","));
        }
        return records;
    }

    @Override
    public ArticleDetailVO getDateils(long id) {
        Article article = articleDao.selectById(id);
        //累加浏览次数
        articleDao.appendBrowse(article);

        ArticleDetailVO convert = dozerUtils.convert(article, ArticleDetailVO.class);

        //查询评论数量
        convert.setCommentSum(commentService.getCount((int) id));
        convert.setComments(commentService.getOk(convert.getArticleId()));
        convert.setArticleTypeArray(convert.getArticleType().split(","));
        convert.setUser(userService.getInfo(convert.getUserId()));
        return convert;
    }

    @Override
    public boolean save(Article article) {
        String s = imagesService.GenerateImage(article.getIcon());
        if (article.getSearchKey() == null){
            article.setSearchKey("无");
        }
        article.setIcon(s);
        article.setUserId(RequestContextUtils.getUser().getUserId());
        return articleDao.insert(article) == 1;
    }

    @Override
    public boolean delete(long id) {
        return articleDao.deleteById(id) == 1;
    }

    @Override
    public boolean praise(long id, boolean type) {
        Article article = new Article();
        article.setArticleId((int) id);
        Article a = articleDao.selectById(id);
        if (type){
            article.setSupport(a.getSupport() + 1);
        }else {
            article.setSupport(a.getSupport() - 1);
        }
        return articleDao.updateById(article) == 1;
    }
}
